package com.netrust.quarz.utils;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.netrust.quarz.common.IConstant;
import com.netrust.quarz.common.enums.OperatorEnums;

/**
 * 项目公共工具类
 * @author Administrator
 *
 */
public class ProdUtils {
	/**
	 * 校验ip是否属于虚拟机ip段
	 * @param ip
	 * @return false不属于。true属于
	 */
	public static boolean checkIp(String ip){
		if(StringUtils.isBlank(ip)){			
			return false;
		}
		if (!ip.startsWith("10.90")
			&&!ip.startsWith("10.91")
			&&!ip.startsWith("10.92")
			&&!ip.startsWith("10.93")
			&&!ip.startsWith("10.94")) {
			return false;
		}
		return true;
	}
	
	public static String op2Ip(String op){
		if(op.equals("ydhlw")){
			return "172.16.101.72";
		}
		if(op.equals("ydzwww")){					
			return "192.168.116.4";			
		}
		if(op.equals("dxhlw")){
			return "172.16.103.72";			
		}
		if(op.equals("dxzwww")){			
			return "2.37.106.234";			
		}
		return "";
	}
	
	public static OperatorEnums op2oe(String op){
		if(op.equals("ydhlw")){
			return OperatorEnums.Operator_yd_hlw;
		}
		if(op.equals("ydzwww")){					
			return  OperatorEnums.Operator_yd_zwww;	
		}
		if(op.equals("dxhlw")){
			return  OperatorEnums.Operator_dx_hlw;		
		}
		if(op.equals("dxzwww")){			
			return  OperatorEnums.Operator_dx_zwww;		
		}
		return OperatorEnums.Operator_yd_hlw;
	}
	
	public static String op2Ckey(String op) {
		if(op.equals("ydhlw")){
			return IConstant.drd_yd_hlw;
		}
		if(op.equals("ydzwww")){					
			return  IConstant.drd_yd_zwww;	
		}
		if(op.equals("dxhlw")){
			return  IConstant.drd_dx_hlw;		
		}
		if(op.equals("dxzwww")){			
			return  IConstant.drd_dx_zwww;		
		}
		return IConstant.drd_dx_zwww;
	}
	
	/**
	 * 根据ip网段分析属于哪个运营商
	 * @param ip
	 * @return
	 */
	public static String operator(String ip){
		if(StringUtils.isBlank(ip)){			
			return "-";
		}else if (ip.startsWith("10.90")){
			return OperatorEnums.Operator_dx_hlw.getName();
		}else if (ip.startsWith("10.91")){
			return OperatorEnums.Operator_dx_zwww.getName();			
		}else if (ip.startsWith("10.92")){
			return OperatorEnums.Operator_yd_hlw.getName();
		}else if (ip.startsWith("10.93")){
			return OperatorEnums.Operator_yd_zwww.getName();
		}
		return "-";
	}
	
	/**
	 * 转换异常信息为为字符串
	 * @param e
	 * @return
	 */
	public static String exception(Exception e){
		StringBuffer sb = new StringBuffer();
		sb.append(e.getMessage()+"\n");
        StackTraceElement[] stackArray = e.getStackTrace();  
        for (int i = 0; i < stackArray.length; i++) {  
            StackTraceElement element = stackArray[i];  
            sb.append(element.toString() + "\n");  
        }  
        return sb.toString();
	}
	
	public static long ipToLong(String ipAddress) {
        long result = 0;
        String[] ipAddressInArray = ipAddress.split("\\.");

        for (int i = 3; i >= 0; i--) {
            long ip = Long.parseLong(ipAddressInArray[3 - i]);
            result |= ip << (i * 8);
        }

        return result;
    }

    public static String longToIp(long ip) {
        StringBuilder sb = new StringBuilder(15);

        for (int i = 0; i < 4; i++) {
            sb.insert(0, Long.toString(ip & 0xff));
            if (i < 3) {
                sb.insert(0, '.');
            }
            ip = ip >> 8;
        }

        return sb.toString();
    }
	
	/**
	 * 从取得的原始json串中获取相关参数
	 * @param jo
	 * @return
	 */
	public static class Usage{
		public static Double disk(JSONObject jo){
			String obj=(String) JSON.parseObject(
										JSON.parseObject(
												JSON.parseArray(
														jo.get("diskstate").toString()
												).get(0).toString()
										).get("metrics").toString()
								).get("diskusage");
			
			
			return StringUtils.isNotEmpty(obj)?Double.valueOf(obj):0.0 ;
		}
		
		public static Double cpu(JSONObject jo){
			String obj=(String) JSON.parseObject(
										JSON.parseObject(
												JSON.parseArray(
															jo.get("virtrualkpi").toString()
														).get(0).toString()
												).get("metrics").toString()
										).get("cpuusage");
			return StringUtils.isNotEmpty(obj)?Double.valueOf(obj):0.0 ;
		}
		
		public static Double mem(JSONObject jo){
			String obj=(String) JSON.parseObject(
										JSON.parseObject(
												JSON.parseArray(
														jo.get("virtrualkpi").toString()
														).get(0).toString()
												).get("metrics").toString()
										).get("memusage");
			return StringUtils.isNotEmpty(obj)?Double.valueOf(obj):0.0 ;
		}
	}
	
	
	@SuppressWarnings({"unchecked", "rawtypes" })
	public static List pushList(List<String> slist) {
		List list=Lists.newArrayList();
		for (String str : slist) {
			if("null"!=str&&StringUtils.isNotBlank(str)){
				if(JSON.parseObject(str).get("alarmList")!=null){
					JSONArray jsa=JSONArray.parseArray(JSON.parseObject(str).get("alarmList").toString());
					for (int j = 0; j < jsa.size(); j++) {
						String ip=JSON.parseObject(jsa.get(j).toString()).getString("ip_addr");
						if (!ProdUtils.checkIp(ip)) {
							continue;
						}
						list.add(jsa.get(j));
					}			
				}
			}			
		}
		
		return list;			
	}

	
	
	
}
